package portal.bean;

import java.io.IOException;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import javax.faces.event.ActionEvent;

import javax.servlet.http.HttpServletRequest;

import oracle.adf.share.ADFContext;

import oracle.adf.view.rich.component.rich.nav.RichCommandLink;


import oracle.webcenter.framework.view.JSFUtils;
import oracle.webcenter.navigationframework.NavigationContext;
import oracle.webcenter.navigationframework.NavigationModel;
import oracle.webcenter.navigationframework.NavigationResource;
import oracle.webcenter.navigationframework.ResourceNotFoundException;
import oracle.webcenter.portalframework.sitestructure.SiteStructure;
import oracle.webcenter.portalframework.sitestructure.SiteStructureContext;
import oracle.webcenter.portalframework.sitestructure.SiteStructureContextState;
import oracle.webcenter.portalframework.sitestructure.SiteStructureResource;
import oracle.webcenter.portalframework.sitestructure.SiteStructureUtils;
import oracle.webcenter.portalframework.sitestructure.handler.CustomViewHandler;


/**
 */
public class LocaleBean {

    private Locale userLocale =
        (Locale)ADFContext.getCurrent().getLocale().clone();

    private static final String JSF_ACCESSOR_NAME = "localeBean";
    private String mainMenuNavigation="";
    private String language="vi";

    public static LocaleBean getCurrentInstance() {
        Map<String, Object> session =
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        LocaleBean localeBean = (LocaleBean)session.get(JSF_ACCESSOR_NAME);

        if (localeBean == null) {
            localeBean = new LocaleBean();
            session.put(JSF_ACCESSOR_NAME, localeBean);
        }

        return localeBean;
    }

    public String changeLocaleAction() {
        redirectToStartPage();
        NavigationBean.getCurrentInstance().setTopnodepath("");
        return "language_" + userLocale.toString();
    }

    public String getSiteStructureParameters() {
        //setActivedPage();
        return "startNode=/" + userLocale.getLanguage() +
               "/" + userLocale.getLanguage() +"menu, includeStartNode=false";
    }

    public void setUserLocale(Locale userLocale) {
        this.userLocale = userLocale;
    }

    public Locale getUserLocale() {
        //redirectToStartPage();
        return userLocale;
    }

    /**
     * Author: TuyếnNT
     * Comment: thủ tục chuyển tới trang chủ tương ứng khi chuyển ngôn ngữ
     */
    private void redirectToStartPage() {
        String strEL =null;                          
        NavigationContext navContext = SiteStructureContext.getInstance();
        NavigationModel navmd;
        try {
            //lay Navigation mac dinh cua he thong
            navmd = navContext.getDefaultNavigationModel();
            //NavigationResource curnode = navmd.getCurrentSelection();
            //lay cac node con của nhánh ngôn ngữ hiện tại
            List<NavigationResource> lsnode = navmd.getResources(null, mainMenuNavigation);
            int i = lsnode.size();
            //dieu huong den node(trang) dau tien cua nhanh
            if (i > 1) {
                NavigationResource node = lsnode.get(1);
                String nodeID = node.getPrettyUrl().substring(node.getPrettyUrl().indexOf("/")+1,node.getPrettyUrl().length());
                strEL ="#{navigationContext.defaultNavigationModel.node['" + node.getPrettyUrl() + "']}";
                SiteStructureResource sStrResource = (SiteStructureResource)JSFUtils.resolveExpression(strEL);
                CustomViewHandler.redirectPrettyUrlIfNeeded(sStrResource);// Only run when parameter "oracle.webcenter.navigationframework.REDIRECT_OPTIONS" was set in web.xml
            }


        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void menuItemClick(ActionEvent actionEvent) {
        // Add event code here...
        RichCommandLink comp = (RichCommandLink)actionEvent.getComponent();
        NavigationResource node =(NavigationResource) comp.getAttributes().get("node");
        String strEL =null;                          
        NavigationContext navContext = SiteStructureContext.getInstance();
        NavigationModel navmd;
        try {
            //lay Navigation mac dinh cua he thong
            navmd = navContext.getDefaultNavigationModel();
          
            //lay cac node con của node duoc click
            List<NavigationResource> lsnode = node.getChildren();
            int i = lsnode.size();
            //dieu huong den node(trang) dau tien cua nhanh
            if (i > 0) {
                NavigationResource nodechidle = lsnode.get(0);
                String nodeID = nodechidle.getPrettyUrl().substring(nodechidle.getPrettyUrl().indexOf("/")+1,nodechidle.getPrettyUrl().length());
                strEL ="#{navigationContext.defaultNavigationModel.node['" + nodechidle.getPrettyUrl() + "']}";
                SiteStructureResource sStrResource = (SiteStructureResource)JSFUtils.resolveExpression(strEL);
                CustomViewHandler.redirectPrettyUrlIfNeeded(sStrResource);// Only run when parameter "oracle.webcenter.navigationframework.REDIRECT_OPTIONS" was set in web.xml
            }


        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void setLanguage(String language) {
        userLocale = new Locale(language);
        this.language = language;
    }

    public String getLanguage() {
        return language;
    }

    public void setMainMenuNavigation(String mainMenuNavigation) {
        this.mainMenuNavigation = mainMenuNavigation;
    }

    public String getMainMenuNavigation() {
        return mainMenuNavigation;
    }
}

